<template>
  <div class="container">
    <div class="dist-area">
      <v-distpicker
        hide-area
        :province="searchParams.province"
        :city="searchParams.city"
        @province="onChangeProvince"
        @city="onChangeCity"
      />
    </div>
    <el-input v-model="searchParams.doAppointEr" placeholder="键入预约者：id\设备id" style="width: 200px" />
    <el-input v-model="searchParams.appointEd" placeholder="键入被预约者：id\设备id" style="width: 200px" />
    <el-button type="primary" icon="el-icon-search" @click="doSearch">检索</el-button>
    <el-table
      v-loading="tableCtl.loading"
      border
      :data="pageList.appointList"
    >
      <el-table-column align="center" width="80px" label="id" prop="appoint.id" />
      <el-table-column label="创建时间">
        <template slot-scope="{row}">
          <span>
            {{ row.appoint.gmtCreate|dateFormat }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="预约时间">
        <template slot-scope="{row}">
          <span>
            {{ row.appoint.appointmentTime | dateFormat }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="区域信息">
        <template slot-scope="{row}">
          <span>
            {{ getLocation(row.doctor.userLocation).province }}
          </span>
          <div>
            {{ getLocation(row.doctor.userLocation).city }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="预约者">
        <template slot-scope="{row}">
          <span>
            预约者：{{ row.appoint.doType === 'DOCTOR'? '医生':'患者' }}
          </span>
          <div>
            id:{{ row.appoint.doAppointEr }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="被预约者">
        <template slot-scope="{row}">
          <span>
            id:{{ row.appoint.appointEd }}
          </span>
        </template>
      </el-table-column>
      <el-table-column label="预约、执行状态">
        <template slot-scope="{row}">
          <span v-if="row.appoint.doAgreement === '1'">
            <el-tag type="success">预约者确认</el-tag>
          </span>
          <span v-if="row.appoint.edAgreement === '1'">
            <el-tag type="success">被预约者确认</el-tag>
          </span>
          <span v-else-if="!row.appoint.edAgreement">
            <el-tag type="warning">被预约者未确认</el-tag>
          </span>
          <span v-else>
            <el-tag size="mediun" type="danger">被预约者已拒绝</el-tag>
          </span>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      :page-sizes="[10, 15, 20, 25]"
      :page-size="pagination.size"
      :current-page.sync="pagination.index"
      :total="pagination.total"
      layout="sizes, prev, pager, next"
      @size-change="handlerPageSizeChange"
      @current-change="handlerPageIndexChange"
    />
  </div>
</template>

<script>
import { req } from '@/api/appoint'
import { getLocation } from '@/utils/loc'
export default {
  data() {
    return {
      searchParams: {
        doAppointEr: null,
        appointEd: null,
        timeStart: null,
        timeEnd: null,
        province: null,
        city: null,
        pageSize: 10,
        pageIndex: 1
      },
      tableCtl: {
        loading: false
      },
      pageList: {
        appointList: null
      },
      dialogShow: {
        userDialog: false,
        equipDialog: false
      },
      dialogDetail: {
        user: null,
        equip: null
      },
      pagination: {
        size: 10,
        index: 1,
        total: 0
      }
    }
  },

  created() {
    this.tableLoading()
    req().then(
      response => {
        console.log(response)
        this.pageList.appointList = response.object.obj
        this.pagination.total = response.object.pageTotal
        this.tableUnLoad()
      }
    )
  },

  methods: {
    tableLoading() {
      this.tableCtl.loading = true
    },
    tableUnLoad() {
      this.tableCtl.loading = false
    },
    onChangeProvince(data) {
      this.searchParams.province = data.value === '省' ? null : data.value
    },
    onChangeCity(data) {
      this.searchParams.city = data.value === '市' ? null : data.value
    },
    handlerPageSizeChange(e) {
      this.pagination.size = e
      this.pagination.index = 1
      this.searchParams.pageSize = e
      this.searchParams.pageIndex = 1
      this.pagination.total = 0
      this.handlerSearch()
    },
    handlerPageIndexChange(e) {
      this.pagination.index = e
      this.searchParams.pageIndex = e
      this.handlerSearch()
    },
    doSearch() {
      if (!this.searchParams.doAppointEr || this.searchParams.doAppointEr.size <= 0) {
        this.searchParams.doAppointEr = null
      }
      if (!this.searchParams.appointEd || this.searchParams.appointEd.size <= 0) {
        this.searchParams.appointEd = null
      }
      this.pagination.total = 0
      this.searchParams.pageIndex = 1
      this.handlerSearch()
    },
    handlerSearch() {
      this.tableLoading()
      req(this.searchParams).then(
        response => {
          this.pageList.appointList = response.object.obj
          this.pagination.total = response.object.pageTotal
          this.tableUnLoad()
        }
      )
    },
    getLocation(data) {
      return getLocation(data)
    },
    clearSearchParam() {
      this.searchParams = {
        doAppointEr: null,
        appointEd: null,
        timeStart: null,
        timeEnd: null,
        province: null,
        city: null,
        pageSize: null,
        pageIndex: null
      }
    }
  }
}
</script>

<style lang="scss">
@import '@/styles/custom.scss';

.container{
    padding: 20px;
}
</style>
